﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using WebApplication;

namespace _1856
{
    public partial class Restart_Game : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Game game = Global.getGame (this);
            
            if (game.Started)
            {
                Response.Redirect("StockRound.aspx");   
            }
            
            if (game != null)
            {
				if (game.RejoinedPlayers != null)
				{
					foreach (string sRejoinedPlayer in game.RejoinedPlayers)
					{
						this.PlayersJoinedLB.Items.Add(sRejoinedPlayer);
					}
				}
                
                foreach (Player player in game.Players)
                {
                    if (!game.RejoinedPlayers.Contains (player.Name))
                    {
                        this.WaitingOnPlayersLB.Items.Add (player.Name);
                    }
                }
            }
            
            if (this.WaitingOnPlayersLB.Items.Count > 0)
            {
                this.RestartGamePB.Enabled = false;
            }
            else
            {
                this.RestartGamePB.Enabled = true;
            }
        }

        protected void RestartGamePB_Click(object sender, EventArgs e)
        {
            if (this.WaitingOnPlayersLB.Items.Count == 0)
            {
                Game game = Global.getGame (this);
                
                if (game != null)
                {
                    game.Started = true;
                }
                
                if (game.InStockRound ())
                {
                    Response.Redirect("StockRound.aspx");   
                }
                else
                {
                    Response.Redirect("OperatingRound2.aspx");
                }
            }
            else
            {
                string  sMessage = "Waiting on the following players: ";
                bool    bFirst = true;
                
                foreach (ListItem item in WaitingOnPlayersLB.Items)
                {
                    if (!bFirst)
                    {
                        sMessage += ", ";
                    }
                    else
                    {
                        bFirst = false;
                    }
                    
                    sMessage += item.Value;
                }
            }
        }


    }
}
